package com.highspeed.bdk.socket.server;

import com.highspeed.bdk.gateway.entity.LuckHeader;
import com.highspeed.bdk.gateway.entity.LuckMessage;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

public class LuckServerDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

        //判断包头大小
        if (in.readableBytes() < 16) {
            return;
        }
        // 包头
        char startTag = in.readChar();
        //序列号
        short reqpacketId = in.readShort();

        short fbpacketId = in.readShort();

        short fbOperType = in.readShort();

        //长度
        int dataLen = in.readInt();
        byte[] content = new byte[dataLen];

        in.readBytes(content);
        int end=  in.readInt();
        if(0xAAAAAAAA!=end){
            return;
        }
        // 组装协议头
        LuckHeader header = new LuckHeader((char) 0xffff, reqpacketId, fbpacketId, fbOperType, dataLen);
//        // 读取消息内容
        LuckMessage message = new LuckMessage(header, content);
        out.add(message);
    }
}